$(function(){
	/*
	 * loading
	 */
	var $loading = $('.overlay').hide();
	var gatherId = $('#gather-id').val();

	/*
	 * 初始化活动详情编辑器
	 */
	var ckeditorConf = {
		language: 'zh-cn',
		toolbar : 'Basic',
		height: 100,
		uiColor : '#FFFFFF',
		extraPlugins : 'uploadimage',
		uploadUrl : '/upload/uploadFile',
		filebrowserUploadUrl : '/upload/uploadFile',
		toolbarGroups : [
			{ name: 'document', groups: [ 'mode', 'document', 'doctools' ] },
			{ name: 'clipboard', groups: [ 'clipboard', 'undo' ] },
			{ name: 'editing', groups: [ 'find', 'selection', 'spellchecker', 'editing' ] },
			{ name: 'forms', groups: [ 'forms' ] },
			'/',
			{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ] },
			{ name: 'paragraph', groups: [ 'list', 'indent', 'blocks', 'align', 'bidi', 'paragraph' ] },
			{ name: 'links', groups: [ 'links' ] },
			{ name: 'insert', groups: [ 'insert' ] },
			'/',
			{ name: 'styles', groups: [ 'styles' ] },
			{ name: 'colors', groups: [ 'colors' ] },
			{ name: 'tools', groups: [ 'tools' ] },
			{ name: 'others', groups: [ 'others' ] },
			{ name: 'about', groups: [ 'about' ] }
		],
		removeButtons : 'Source,Save,NewPage,Preview,Print,Templates,Cut,Copy,Paste,PasteText,PasteFromWord,Undo,Redo,Replace,Find,SelectAll,Scayt,Form,Checkbox,Radio,TextField,Textarea,Select,Button,ImageButton,HiddenField,Italic,Underline,Strike,Subscript,Superscript,RemoveFormat,NumberedList,BulletedList,Outdent,Indent,Blockquote,CreateDiv,JustifyLeft,BidiLtr,Link,Image,JustifyCenter,BidiRtl,Flash,Unlink,Anchor,Language,JustifyRight,JustifyBlock,Table,HorizontalRule,Smiley,SpecialChar,PageBreak,Iframe,FontSize,Font,Format,Styles,TextColor,BGColor,ShowBlocks,Maximize,About'
	}
	
	var editorMap = {};
	var adviceArr = [];
	$('textarea').each(function(index){
		var $textarea = $(this);
		$textarea.attr('id','editor'+index);
	});
	$('textarea').not('#share-and-advice textarea').each(function(){
		var $textarea = $(this);
		editorMap[$textarea.attr('name')] = CKEDITOR.replace($textarea.attr('id'),ckeditorConf);
	});
	$('#share-and-advice .form-group').each(function(){
		var $this = $(this);
		var $textarea = $this.find('textarea');
		var obj = {
			ele: $this,
			editor: CKEDITOR.replace($textarea.attr('id'),ckeditorConf)
		};
		adviceArr.push(obj);
	});

	$('#save-btn').on('click',function(){
		save();
	});

	/*
	 * 保存资讯
	 */
	function save() {
		var formData = {};
		for( var key in editorMap ) {
			formData[key] = editorMap[key].getData();
		}
		var adviceDataArr = [];
		for( var index in adviceArr ) {
			var obj = adviceArr[index];
			var $ele = obj.ele;
			adviceDataArr.push({
				id:$ele.find('[name="id"]').val(), 
				adviceTitle: $ele.find('[name="adviceTitle"]').val(),
				adviceCon: obj.editor.getData()
			});
		}
		formData['gatherMember'] = JSON.stringify(adviceDataArr);
		$('input').not('#share-and-advice input').each(function(){
			var $this = $(this);	
			formData[$this.attr('name')] = $this.val();
		});
		
		console.log(formData);
		cAjax({
			url: '/gather/gathering/gather/editLookback',
			data: formData,
			dataType: 'json',
			cache: false,
			success: function(obj) {
				greeting({
					msg: '保存成功'
				});
		   	}
		});
	}

	/*
	 * 发布&取消
	 */
	$('.pub-btns').on('click','#publish-btn',function() {
		pub.call($(this),1);
	});

	$('.pub-btns').on('click','#cancel-publish-btn',function() {
		pub.call($(this),0);
	});

	var $pubBtns = $('.pub-btn');
	function pub(status) {
		var msg = (status == 1) ? '确认发布这篇回顾吗？' : '确认取消发布这篇回顾吗？';
		actionConfirm({
			msg: msg,
			confirm: function() {
				cAjax({
					url: '/gather/gathering/gather/lookbackStatus',
					type: 'get',
					data: {
						id: gatherId,
						lookbackStatus: status
					},
					success: function(obj) {
						$pubBtns.toggle();
					}
				});	
			}
		});
	}

});
